package weaver.jobs.zhlx.duss.cron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.zhlx.duss.utils.ZhlxJobUtil;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-08-29
 * @Description: 定时调用SAP成本中心接口同步供应商主数据到建模表单
 * @ModifyLog:
 **/

public class WeaSyncCbzxBySapCron extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            ZhlxJobUtil jobUtil = new ZhlxJobUtil();
            String tablename = "uf_jcdjcbzxtz";
            int formmodeid = jobUtil.getFormmodeid(tablename);
            JSONObject dataJson = new JSONObject();
            dataJson.put("BUKRS", "");
            dataJson.put("KOSTL", "");
            JSONObject resultJson = jobUtil.handleOKHttpRequest("CO001", dataJson);
            String code = Util.null2String(resultJson.getString("code"));
            baseBean.writeLog(">>>>>WeaSyncCbzxBySapCron>>>>>code=" + code);
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String msgText = outputJson.getString("MSG_TEXT");
                baseBean.writeLog(">>>>>WeaSyncCbzxBySapCron>>>>>msgType=" + msgType + ",msgText=" + msgText);
                if ("S".equals(msgType) && "调用成功".equals(msgText)) {
                    String dataReturnStr = outputJson.getString("DATA");
                    JSONObject dataReturnJson = JSONObject.parseObject(dataReturnStr);
                    String returnData = dataReturnJson.getString("RETURN");
                    JSONArray returnList = JSONArray.parseArray(returnData);
                    for (int i = 0; i < returnList.size(); i++) {
                        JSONObject paramJson = new JSONObject();
                        JSONObject returnJson = returnList.getJSONObject(i);
                        paramJson.put("gsdm", returnJson.getString("BUKRS"));
                        paramJson.put("gsmc", returnJson.getString("BUTXT"));
                        String cbzxbm = returnJson.getString("KOSTL");
                        paramJson.put("cbzxbm", cbzxbm);
                        paramJson.put("cbzxmc", returnJson.getString("LTEXT"));
                        paramJson.put("lrzx", returnJson.getString("PRCTR"));
                        paramJson.put("kssx_rq", returnJson.getString("DATAB"));
                        paramJson.put("yxjz_rq", returnJson.getString("DATBI"));
                        paramJson.put("gnfw", returnJson.getString("FUNC_AREA"));
                        paramJson.put("hbm", returnJson.getString("WAERS"));
                        paramJson.put("bm", returnJson.getString("ABTEI"));
                        paramJson.put("jxtbm", returnJson.getString("DATLT"));
                        paramJson.put("sfdj", returnJson.getString("BKZKP"));
                        paramJson.put("cj_rq", returnJson.getString("ERSDA"));
                        int mainid = jobUtil.getMainId(tablename, "cbzxbm", cbzxbm);
                        if (mainid <= 0) {
                            jobUtil.insertModelData(formmodeid, paramJson);
                        } else {
                            jobUtil.updateModelData(tablename, mainid, paramJson);
                        }
                    }
                }
            }
        } catch (Exception e) {
            baseBean.writeLog(">>>>>WeaSyncCbzxBySapCron>>>>>Exception=" + e);
        }

    }


}

